<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小区名" prop="name">
      <el-input v-model="dataForm.name" placeholder="小区名"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="province">
      <el-input v-model="dataForm.province" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="区域" prop="area">
      <el-input v-model="dataForm.area" placeholder="区域"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="latitude">
      <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度(GPS)" prop="latitudeGps">
      <el-input v-model="dataForm.latitudeGps" placeholder="纬度(GPS)"></el-input>
    </el-form-item>
    <el-form-item label="经度(GPS)" prop="longitudeGps">
      <el-input v-model="dataForm.longitudeGps" placeholder="经度(GPS)"></el-input>
    </el-form-item>
    <el-form-item label="物业类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="物业类型"></el-input>
    </el-form-item>
    <el-form-item label="物业费" prop="managementFee">
      <el-input v-model="dataForm.managementFee" placeholder="物业费"></el-input>
    </el-form-item>
    <el-form-item label="总建面积" prop="size">
      <el-input v-model="dataForm.size" placeholder="总建面积"></el-input>
    </el-form-item>
    <el-form-item label="总户数" prop="sum">
      <el-input v-model="dataForm.sum" placeholder="总户数"></el-input>
    </el-form-item>
    <el-form-item label="建造年代" prop="age">
      <el-input v-model="dataForm.age" placeholder="建造年代"></el-input>
    </el-form-item>
    <el-form-item label="停车位" prop="car">
      <el-input v-model="dataForm.car" placeholder="停车位"></el-input>
    </el-form-item>
    <el-form-item label="容积率" prop="rlv">
      <el-input v-model="dataForm.rlv" placeholder="容积率"></el-input>
    </el-form-item>
    <el-form-item label="绿化率" prop="llv">
      <el-input v-model="dataForm.llv" placeholder="绿化率"></el-input>
    </el-form-item>
    <el-form-item label="开发商" prop="producer">
      <el-input v-model="dataForm.producer" placeholder="开发商"></el-input>
    </el-form-item>
    <el-form-item label="物业公司" prop="management">
      <el-input v-model="dataForm.management" placeholder="物业公司"></el-input>
    </el-form-item>
    <el-form-item label="相关学校" prop="school">
      <el-input v-model="dataForm.school" placeholder="相关学校"></el-input>
    </el-form-item>
    <el-form-item label="小区介绍" prop="info">
      <el-input v-model="dataForm.info" placeholder="小区介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          province: '',
          city: '',
          area: '',
          address: '',
          latitude: '',
          longitude: '',
          latitudeGps: '',
          longitudeGps: '',
          type: '',
          managementFee: '',
          size: '',
          sum: '',
          age: '',
          car: '',
          rlv: '',
          llv: '',
          producer: '',
          management: '',
          school: '',
          info: ''
        },
        dataRule: {
          name: [
            { required: true, message: '小区名不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '区域不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          latitudeGps: [
            { required: true, message: '纬度(GPS)不能为空', trigger: 'blur' }
          ],
          longitudeGps: [
            { required: true, message: '经度(GPS)不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '物业类型不能为空', trigger: 'blur' }
          ],
          managementFee: [
            { required: true, message: '物业费不能为空', trigger: 'blur' }
          ],
          size: [
            { required: true, message: '总建面积不能为空', trigger: 'blur' }
          ],
          sum: [
            { required: true, message: '总户数不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '建造年代不能为空', trigger: 'blur' }
          ],
          car: [
            { required: true, message: '停车位不能为空', trigger: 'blur' }
          ],
          rlv: [
            { required: true, message: '容积率不能为空', trigger: 'blur' }
          ],
          llv: [
            { required: true, message: '绿化率不能为空', trigger: 'blur' }
          ],
          producer: [
            { required: true, message: '开发商不能为空', trigger: 'blur' }
          ],
          management: [
            { required: true, message: '物业公司不能为空', trigger: 'blur' }
          ],
          school: [
            { required: true, message: '相关学校不能为空', trigger: 'blur' }
          ],
          info: [
            { required: true, message: '小区介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/housecommunity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.houseCommunity.name
                this.dataForm.province = data.houseCommunity.province
                this.dataForm.city = data.houseCommunity.city
                this.dataForm.area = data.houseCommunity.area
                this.dataForm.address = data.houseCommunity.address
                this.dataForm.latitude = data.houseCommunity.latitude
                this.dataForm.longitude = data.houseCommunity.longitude
                this.dataForm.latitudeGps = data.houseCommunity.latitudeGps
                this.dataForm.longitudeGps = data.houseCommunity.longitudeGps
                this.dataForm.type = data.houseCommunity.type
                this.dataForm.managementFee = data.houseCommunity.managementFee
                this.dataForm.size = data.houseCommunity.size
                this.dataForm.sum = data.houseCommunity.sum
                this.dataForm.age = data.houseCommunity.age
                this.dataForm.car = data.houseCommunity.car
                this.dataForm.rlv = data.houseCommunity.rlv
                this.dataForm.llv = data.houseCommunity.llv
                this.dataForm.producer = data.houseCommunity.producer
                this.dataForm.management = data.houseCommunity.management
                this.dataForm.school = data.houseCommunity.school
                this.dataForm.info = data.houseCommunity.info
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/housecommunity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'area': this.dataForm.area,
                'address': this.dataForm.address,
                'latitude': this.dataForm.latitude,
                'longitude': this.dataForm.longitude,
                'latitudeGps': this.dataForm.latitudeGps,
                'longitudeGps': this.dataForm.longitudeGps,
                'type': this.dataForm.type,
                'managementFee': this.dataForm.managementFee,
                'size': this.dataForm.size,
                'sum': this.dataForm.sum,
                'age': this.dataForm.age,
                'car': this.dataForm.car,
                'rlv': this.dataForm.rlv,
                'llv': this.dataForm.llv,
                'producer': this.dataForm.producer,
                'management': this.dataForm.management,
                'school': this.dataForm.school,
                'info': this.dataForm.info
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
